﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DAL
{
    public class Until
    {
        /*
         * Chuẫn hóa chuỗi ký tự
         */
        public static String StandardizedInput(String input)
        {
            String s = null;
            char[] SpecialKey = new char[] { '~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '|', '{', '[', '}', ']', ';', ':', '"', ',', '<', '>', '.', ' ' };
            string[] NewArray = input.Split(SpecialKey, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < NewArray.Length; i++)
            {
                if (i < NewArray.Length - 1)
                {
                    s += NewArray[i]+" ";
                }
                if (i == NewArray.Length - 1)
                {
                    s += NewArray[i];
                }
            }
            return s;
         
        }
        public static String StandardizedInput(String input, String Prefix)
        {
            String temp = input;
            String newString = null;
            try
            {
                //kiem tra co dau phay hay khong        
                if (IsPrefix(input, Prefix))
                {
                    //remove het chuoi 
                    Char[] s = input.ToCharArray();
                    for (int i = 0; i < s.Length; i++)
                    {
                        if (s[i] == 44)
                        {
                            if (s[i - 1] == 32 && s[i + 1] == 32)
                            {
                                for (int j = i; j < s.Length; j++)
                                {
                                    if (s[j] != 32 && s[j] != 44)
                                    {
                                        break;
                                    }
                                    if (s[j] == 32)
                                    {
                                        i++;
                                    }
                                }

                                continue;
                            }
                            if ((s[i - 1] == 32 && s[i + 1] != 32) || (s[i - 1] != 32 && s[i + 1] == 32))
                            {
                                continue;
                            }
                            else
                            {
                                newString += " ";
                            }
                        }
                        else
                        {
                            newString += s[i];
                        }
                    }

                }
                temp = newString;
            }
            catch { return null; }
            return temp;
        }
        public static bool IsPrefix(String input, String Prefix)
        {
            int i = -1;
            i = input.IndexOf(Prefix);
            if (i > -1)
                return true;
            else
                return false;
        }
       /*
        * Chuyển chuỗi sang mang các từ
        */
        public static String[] ConvertStringToArray(String input)
        {
            String temp = input;
            String[] s = new String[CountPlace(input) + 1];
            int i = 0;
            String a = null;
            char[] mang = input.ToCharArray();
            int m = 0;
            int leghtn = input.Length;
            for (int bg = 0; bg < leghtn; bg++)
            {

                char c = mang[bg];
                if (c == 32 || m == leghtn)
                {
                    s[i] = a;
                    a = null;
                    i++;
                }
                else
                {
                    a += c;

                }
                m++;
            }
            s[i] = a;
            return s;
        }
        public static String[] ConvertStringToArray2(String input)
        {
            char[] SpecialKey = new char[] { '~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '|', '{', '[', '}', ']', ';', ':', '"', ',', '<', '>', '.', ' ' };
            string[] NewArray = input.Split(SpecialKey, StringSplitOptions.RemoveEmptyEntries);
            return NewArray;
        }
        public static int CountPlace(String input)
        {
            int num = 0;
            foreach (char c in input)
            {
                if (c == 32)
                    num++;

            }
            return num;
        }
        /*
         * Thực hiện tạo where  
         */
        public static String CreateWhere(String[] s, String tencomlum)
        {

            String rs = null;
            try
            {
                for (int i = 0; i < s.Length; i++)
                {
                    if (i == 0)
                    {
                        rs += tencomlum + " like '%" + s[i] + "%' or ";
                    }
                    else
                    {
                        if (i < s.Length - 1)
                        {
                            rs += tencomlum + " like '%" + s[i] + "%' or ";
                        }
                        else
                        {
                            rs += tencomlum + " like '%" + s[i] + "%'  ";
                        }
                    }
                }
            }
            catch { return null; }
            return rs;
        }
        public static String CreateWhere2(String[] s, String tenComlumn, int start, int end)
        {
            String rs = null;
            String tw = "";
            try
            {
                for (int i = start; i < start + 2; i++)
                {
                    if (i == start)
                    {
                        if (end - start == 0)
                        {
                            tw += s[i];
                            rs += tenComlumn + " like '%" + s[i] + "%' ";
                        }
                        else
                        {
                            if (end - start == 1)
                            {
                                tw += s[i];
                                rs += tenComlumn + " like '%" + s[i] + "%' or ";
                            }

                            else
                            {
                                tw += s[i] + " ";
                                rs += tenComlumn + " like '%" + s[i] + "%' or ";
                            }
                        }

                    }
                    else
                    {
                        //thuc hien lay tu ky tu thu 1 +2 
                        for (int j = i; j < end + 1; j++)
                        {
                            if (j < end)
                            {
                                tw += s[j] + " ";
                                rs += tenComlumn + " like '%" + tw + "%' or ";
                            }
                            else
                            {
                                tw += s[j] + " ";
                                rs += tenComlumn + " like '%" + tw + "%' ";

                            }
                        }
                    }
                }
            }
            catch { return null; }
            return rs;

        }
        public static void AddData(DataRow dr, ref DataTable dt, String ColumnFillter, String ColumnValue)
        {
            DataView dv = dt.DefaultView;
            String s = dr[0].ToString();
            try
            {
                if (dv.Count != 0)
                {
                    dv.Sort = ColumnFillter;
                    int pos = dv.Find(s);
                    if (pos != -1)
                    {
                        if (dv[pos].Row[ColumnFillter].ToString() == dr[ColumnValue].ToString())
                        {
                            return;
                        }
                    }
                    else
                    {
                        DataRow newrow = dt.NewRow();
                        newrow[ColumnFillter] = dr[ColumnValue].ToString();
                        dt.Rows.Add(newrow);
                    }
                }
                else
                {
                    DataRow newrow = dt.NewRow();
                    newrow[ColumnFillter] = dr[ColumnValue].ToString();
                    dt.Rows.Add(newrow);
                }
            }
            catch { return; }

        }
        public static List<DataRow> Paging(DataTable tbl, int start, int total)
        {
            int i;
            int totalRows = tbl.Rows.Count;
            if (start > totalRows || start < 0)
                return null;
            else
            {
                int positioncontinue = 0;
                if (start == 1)
                    positioncontinue = 0;
                else
                    positioncontinue = (start-1) * 5;
                List<DataRow> result = new List<DataRow>();
                int length = 0;
                if (start == 1)
                {
                    length = 5;
                }
                else
                {
                    if (((start-1) * 5) < tbl.Rows.Count)
                    {
                        length = ((start-1) * 5) + total;
                    }
                    if(((start-1)*5)==totalRows)
                    {
                        length=(start-1)*5;
                    }
                    if (((start-1) * 5) > totalRows)
                    {
                        length = totalRows - positioncontinue;
                    }
                }
               
                if (length > totalRows)
                    length = totalRows;
                for (i = positioncontinue; i < length; i++)
                {
                    result.Add(tbl.Rows[i]);
                }
                return result;
            }

        }

        public static String ConvertStringToArraykhoa(String input)
        {
            char[] SpecialKey = new char[] { '~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '|', '{', '[', '}', ']', ';', ':', '"', ',', '<', '>', '.'};
            string[] NewArray = input.Split(SpecialKey, StringSplitOptions.RemoveEmptyEntries);
            String chuoi = "";
            for (int i = 0; i < NewArray.Length; i++)
            {
                if (NewArray[i] != "")
                {
                    chuoi += NewArray[i];
                    if (i != NewArray.Length - 1)
                        chuoi += "---,---";
                }
            }
            return chuoi;
        }
        public static void MegeTable(ref DataTable dich,DataTable nguon)
        {
          
            foreach (DataRow drs in nguon.Rows)
            {
                DataRow dr = dich.NewRow();
                dr["TenDiaDiem"] = drs["TenDiaDiem"].ToString();
                dr["SoNha"] = drs["SoNha"].ToString();
                dr["TenDuong"] = drs["TenDuong"].ToString();
                dr["TenPhuong"] = drs["TenPhuong"].ToString();
                dr["TenQuanHuyen"] = drs["TenQuanHuyen"].ToString();
                dr["TenTinhThanh"] = drs["TenTinhThanh"].ToString();
                dr["ChuThich"] = drs["ChuThich"].ToString();
                dr["MaTenDiaDiem"] = drs["MaTenDiaDiem"].ToString();
                dr["KinhDo"] = drs["KinhDo"].ToString();
                dr["ViDo"] = drs["ViDo"].ToString();
                dr["TenDichVu"] = drs["TenDichVu"].ToString();
                dich.Rows.Add(dr);
            }
        }
        public static bool IsFull(DataTable dt,int numbermax)
        {
            if (dt.Rows.Count >= numbermax)
                return true;
            else
                return false;
        }
        // tach bang thanh danh sach  bang 
        public static List<DataTable> DSBANG(DataTable dt,int numbermax)
        {
            List<DataTable> dscantim = new List<DataTable>();
            //lap danh sach tim so dong lơn hơn 200  
            int sodong = dt.Rows.Count;
            int solap = sodong / numbermax;
            if (solap * numbermax < dt.Rows.Count)
                solap++;
            for (int i = 0; i < solap; i++)
            { 
                dscantim.Add(GetThread(dt,i,numbermax));
            }
            return dscantim;
        }
        public static String GetTableName(DataTable dt)
        {
            return dt.TableName;
        }
        public static DataTable GetThread(DataTable dts,int page,int numbermax)
        {
            DataTable dt = new DataTable();
            DataColumn[] cl = new DataColumn[] { new DataColumn("MaTenDiaDiem") };
            dt.Columns.AddRange(cl);
            int count = dts.Rows.Count;
            int position = 0;
            int last =0;
            if (count > numbermax)
            {
                if (page == 0)
                {
                    position = 0;
                    last = 200;
                }
                else
                {
                    if (page == 1)
                    {
                        position = 200;
                        if ((page+1 )* numbermax < dts.Rows.Count)
                        {
                            last = numbermax * (page + 1);

                        }
                        else
                        {
                            last = dts.Rows.Count;
                        }
                    }
                    else
                    {
                        position = (page - 1) * 200;
                        if (position + 200 >= count)
                        {
                            last = count - position;
                        }
                        else
                        {
                            last = position + 200;
                        }
                    }
                }
                int numbercomlum = dts.Columns.Count;
                for (int i = position; i < last; i++)
                {
                   
                        DataRow dr = dt.NewRow();
                        DataRow dk = dts.Rows[i];
                        dr["MaTenDiaDiem"] = dk["MaTenDiaDiem"].ToString();
                        dt.Rows.Add(dr);
                    
                }
            }
            else
            {
                return dts;
            }
          return dt;
        }
    }
}
